package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.service.EditQuestionService;
import com.cslg.questionnaires.utils.JsonMsgBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class StudentEditQuestionController {
    @Autowired
    EditQuestionService editQuestionService;

    @GetMapping("/StudentEditQuestion/{questionId}")
    public ModelAndView editQuestionPage(@PathVariable("questionId") int questionId, HttpSession session){
        Student student = (Student) session.getAttribute("user");
        String userType = (String) session.getAttribute("userType");
        int userId = student.getId();
        boolean flag = editQuestionService.canQuestionEdit(questionId,userId,userType);
        if (flag){
            HashMap<String,Object> data = editQuestionService.getEditQuesiton(questionId);
            HashMap<String,Object> msg = (HashMap<String, Object>)data.get("data");
            msg.put("userType",userType);
            msg.put("username",student.getStu_name());
            msg.put("account",student.getStu_id());
            return new ModelAndView("EditQuestion",(HashMap<String, Object>)data.get("data"));
        }else{
            return new ModelAndView("redirect:/"+userType+"Home?page=1");
        }
    }

    @ResponseBody
    @PostMapping("/StudentEditQuestion/save/{questionId}")
    public HashMap<String,Object> saveEditQuestion(@PathVariable("questionId") int questionId, @RequestBody HashMap<String,Object> request, HttpSession session){
        Student student = (Student) session.getAttribute("user");
        String userType = (String) session.getAttribute("userType");
        int userId = student.getId();
        boolean flag = editQuestionService.canQuestionEdit(questionId,userId,userType);
        if (flag) {
            HashMap<String, Object> msg = editQuestionService.saveEditQuestion(request,questionId,userId);
            return msg;
        } else {
            return JsonMsgBuilder.jsonBuild(404,"未找到匹配表单！");
        }
    }

    @ResponseBody
    @PostMapping("/StudentEditQuestion/publish/{questionId}")
    public HashMap<String,Object> publishEditQuesiton(@PathVariable("questionId") int questionId, @RequestBody HashMap<String,Object> request, HttpSession session){
        Student student = (Student) session.getAttribute("user");
        String userType = (String) session.getAttribute("userType");
        int userId = student.getId();
        boolean flag = editQuestionService.canQuestionEdit(questionId,userId,userType);
        if (flag) {
            HashMap<String, Object> msg = editQuestionService.publishEditQuestion(request,questionId,userId);
            return msg;
        } else {
            return JsonMsgBuilder.jsonBuild(404,"未找到匹配表单！");
        }
    }
}
